package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.result.Result;
import com.example.demo.result.ResultFactory;
import com.example.demo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/money")
public class ShopOrecharController {
    @Autowired
    OrderService orderService;

    @Autowired
    UserService userService;

    @Autowired
    ShopService shopService;

    @Autowired
    BookService bookService;

    @Autowired
    ReckoningService reckoningService;

    @Autowired
    BillService billService;

    @PostMapping("/order")
    @ResponseBody
    public Result addOrder(@RequestBody Bill requestBill){
        try {
            int orderNumber=requestBill.getOrderNumber();
            int bookId=requestBill.getBookId();
            int bnumber=bookService.selectBookNB(bookId);
            if(bnumber<orderNumber)
                return ResultFactory.buildFailResult("库存不足!");
            String customerId = requestBill.getCustomerId();
            String balance = userService.selectBalance(customerId);
            String money = requestBill.getMoney();
            if (Double.parseDouble(balance) - Double.parseDouble(money) < 0)
                return ResultFactory.buildFailResult("余额不足!");
            String bookName=requestBill.getBookName();
            String price=requestBill.getPrice();
            String date=requestBill.getDate();
            String address=requestBill.getAddress();
            String phoneNumber=requestBill.getPhoneNumber();
            int deliveryId=requestBill.getDeliveryId();
            Order order = new Order();
            order.setCustomerId(customerId);
            order.setBookId(bookId);
            order.setOrderNumber(orderNumber);
            order.setMoney(money);
            order.setDate(date);
            orderService.insertOrder(order);
            userService.updateBalance(money, customerId);
            bookService.updateBNumber(order.getOrderNumber(), order.getBookId());
            Reckoning reckoning=new Reckoning();
            reckoning.setCustomerId(customerId);
            reckoning.setMoney(money);
            reckoning.setDate(date);
            reckoningService.insertReckoning(reckoning);
            Bill bill=new Bill();
            bill.setAddress(address);
            bill.setBookId(bookId);
            bill.setBookName(bookName);
            bill.setCustomerId(customerId);
            bill.setDate(date);
            bill.setDeliveryId(deliveryId);
            bill.setMoney(money);
            bill.setOrderNumber(orderNumber);
            bill.setPhoneNumber(phoneNumber);
            bill.setPrice(price);
            billService.insertBill(bill);
            return ResultFactory.buildSuccessResult(customerId);
        }catch (Exception e){
            return ResultFactory.buildFailResult("错误!");
        }
    }

    @PostMapping("/shop")
    @ResponseBody
    public Result addShop(@RequestBody Map<String,String> map){
        Shop shop=new Shop();
        shop.setCustomerId(map.get("CustomerId"));
        shop.setBookId((Integer.parseInt(map.get("BookId"))));
        shop.setOrderNumber((Integer.parseInt(map.get("OrderNumber"))));
        shop.setMoney(map.get("Money"));
        shopService.insertShop(shop);
        return ResultFactory.buildSuccessResult(shop.getCustomerId());
    }

    @PostMapping("/selectShop1/{id}")
    public Result selectShop1(@PathVariable("id") String id){
        List<Shop1> shop1s=shopService.selectShop1(id);
        return ResultFactory.buildSuccessResult(shop1s);
    }

    @GetMapping("/selectOrder2")
    public Result selectOrder2(){
        List<Order1> order1s=orderService.selectOrder2();
        return ResultFactory.buildSuccessResult(order1s);
    }

    @PostMapping("/selectOrder3/{name}")
    public Result selectOrder3(@PathVariable("name") String name){
        List<Order1> order1s=orderService.selectOrder3(name);
        return ResultFactory.buildSuccessResult(order1s);
    }

    @PostMapping("/selectOrder4/{id}")
    public Result selectOrder4(@PathVariable("id") String id){
        List<Order1> order1s=orderService.selectOrder4(id);
        return ResultFactory.buildSuccessResult(order1s);
    }

    @PostMapping("/deleteShop1/{id}")
    public Result deleteShop1(@PathVariable("id") String id){
        shopService.deleteShop1(id);
        return ResultFactory.buildSuccessResult(id);
    }

    @PostMapping("/insertShop")
    public Result insertShop(@RequestBody Map<String,String> map){
        System.out.println(map);
        User user=userService.selectById1(map.get("customerId"));
        Book book=bookService.selectById(map.get("bookId"));
        Order order=new Order();
        order.setCustomerId(map.get("customerId"));
        order.setBookId(Integer.parseInt(map.get("bookId")));
        order.setOrderNumber(Integer.parseInt(map.get("orderNumber")));
        order.setMoney(map.get("money"));
        order.setDate(map.get("date"));
        int number=bookService.selectById(String.valueOf(order.getBookId())).getBNumber();
        if(number<order.getOrderNumber())
            return ResultFactory.buildFailResult("库存不足!");
        int balance=Integer.parseInt(userService.selectBalance(order.getCustomerId()));
        double money=Double.parseDouble(order.getMoney());
        if(balance<money)
            return ResultFactory.buildFailResult("余额不足!");
        orderService.insertOrder(order);
        Bill bill=new Bill();
        bill.setAddress(user.getAddress());
        bill.setBookId(Integer.parseInt(map.get("bookId")));
        bill.setBookName(map.get("bookName"));
        bill.setCustomerId(map.get("customerId"));
        bill.setDate(map.get("date"));
        bill.setDeliveryId(Integer.parseInt(map.get("deliveryId")));
        bill.setMoney(map.get("money"));
        bill.setOrderNumber(Integer.parseInt(map.get("orderNumber")));
        bill.setPhoneNumber(user.getPhoneNumber());
        bill.setPrice(book.getPrice());
        billService.insertBill(bill);
        return ResultFactory.buildSuccessResult(1);
    }

    @PostMapping("/selectOrder/{id}")
    public Result selectOrder(@PathVariable("id") String id){
        List<Order> orders=orderService.selectOrder(id);
        return ResultFactory.buildSuccessResult(orders);
    }

    @GetMapping("/selectO")
    public Result selectO(){
        List<Order> orders=orderService.selectO();
        return ResultFactory.buildSuccessResult(orders);
    }

    @PostMapping("/deleteOrder/{id}")
    public Result deleteOrder(@PathVariable("id") String id){
        orderService.deleteOrder(id);
        return ResultFactory.buildSuccessResult(id);
    }

    @GetMapping("/statistics")
    public Result statistics(){
        Map<String,String> map=orderService.statistics();
        return ResultFactory.buildSuccessResult(map);
    }

    @GetMapping("/statistics1")
    public Result statistics1(){
        List<Order3> map=orderService.statistics1();
        return ResultFactory.buildSuccessResult(map);
    }

    @GetMapping("/analysis")
    public Result analysis(){
        Map<String,String> map=orderService.analysis();
        return ResultFactory.buildSuccessResult(map);
    }

    @GetMapping("/analysis1")
    public Result analysis1(){
        List<Order2> map=orderService.analysis1();
        return ResultFactory.buildSuccessResult(map);
    }

}
